package com.easylinkin.linkappapi.asset.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.asset.entity.AdeviceVo;
import com.easylinkin.linkappapi.asset.entity.Asset;
import com.easylinkin.linkappapi.asset.entity.AssetRefDevice;
import com.easylinkin.linkappapi.asset.mapper.AssetMapper;
import com.easylinkin.linkappapi.asset.mapper.AssetRefDeviceMapper;
import com.easylinkin.linkappapi.asset.service.AssetRefDeviceService;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.device.mapper.DeviceMapper;
import com.easylinkin.linkappapi.space.entity.LinkappArea;
import com.easylinkin.linkappapi.space.mapper.LinkappAreaMapper;

/**
 * <p>
 * 设备资产关联用户 服务实现类
 * </p>
 *
 * @author XuGuoQing
 * @since 2020-04-15
 */
@Service
public class AssetRefDeviceServiceImpl extends ServiceImpl<AssetRefDeviceMapper, AssetRefDevice> implements AssetRefDeviceService {

    static final int NO_DELETE = 1;

    @Resource
    DeviceMapper deviceMapper;

    @Autowired
    private AssetMapper assetMapper;

    @Override
    public int batchDelete(List<String> assetId) {
        QueryWrapper<AssetRefDevice> wrapper = new QueryWrapper<>();
        wrapper.in("asset_id", assetId);
        return baseMapper.delete(wrapper);
    }


    @Override
    public void deleteByDeviceIds(String assetId, List<String> deviceIdList) {
        QueryWrapper<AssetRefDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("asset_id", assetId).in("device_id", deviceIdList);
        baseMapper.delete(wrapper);
    }


    @Override
    public boolean batchAdd(String assetId, List<String> deviceIdList) {
        if (ObjectUtils.isEmpty(deviceIdList)) {
            return true;
        }
        deviceIdList = deviceIdList.stream().distinct().collect(Collectors.toList());
        deviceIdList = getExistedDeviceId(deviceIdList);
        if (ObjectUtils.isEmpty(deviceIdList)) {
            return true;
        }
        List<AssetRefDevice> assetRefManagerList = new ArrayList<>(15);
        for (String deviceId : deviceIdList) {
            assetRefManagerList.add(new AssetRefDevice(assetId, deviceId));
        }
        return this.saveBatch(assetRefManagerList);
    }

    @Override
    public List<String> isAssetRefDevices(String assetId, List<String> addeviceIdList, List<String> deletedeviceIdList) {
        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put("asset_id", assetId);
        List<AssetRefDevice> assetIdDeviceIdList = baseMapper.selectByMap(columnMap);
        List<String> existDeviceIdList = new ArrayList<>();
        //需要更新的情况
        if (!ObjectUtils.isEmpty(addeviceIdList) || !ObjectUtils.isEmpty(deletedeviceIdList)) {
            existDeviceIdList = assetIdDeviceIdList.stream().map(AssetRefDevice::getDeviceId).collect(Collectors.toList());
            existDeviceIdList.addAll(addeviceIdList);
            existDeviceIdList.removeAll(deletedeviceIdList);
            existDeviceIdList = existDeviceIdList.stream().distinct().collect(Collectors.toList());
        }
        return existDeviceIdList;
    }


    @Override
    public List<String> getExistedDeviceId(List<String> deviceIdList) {
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.in("id", deviceIdList);
        List<Device> deviceList = deviceMapper.selectList(wrapper);
        return deviceList.stream().map(Device::getId).collect(Collectors.toList());

    }

    @Autowired
    private LinkappAreaMapper areaMapper;

    @Override
    public List<String> getDeviceIdsByAssetId(String assetId) {

        Asset asset = assetMapper.selectById(assetId);

        QueryWrapper<LinkappArea> qw = new QueryWrapper<>();
        qw.eq("id", asset.getAssetAreaId());
        qw.select("area_path");
        LinkappArea area = areaMapper.selectOne(qw);

        AdeviceVo adeviceVo = new AdeviceVo();
        adeviceVo.setAssetId(assetId);
        adeviceVo.setAreaPath(area.getAreaPath());
        List<Device> datas = assetMapper.getDvicePageByAssetId(adeviceVo);
        List<String> deviceIds = datas.stream().map(Device::getId).collect(Collectors.toList());
        return deviceIds;
    }


}
